package com.eascs.demand.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.demand.entity.MainApply;

@Service
public class TicketMainApplyService extends BaseSevice{
	private static Logger log = LoggerFactory.getLogger(TicketMainApplyService.class);
	@Autowired
	private BaseUserUtil userUtils;
	//查询主表
	@Transactional(transactionManager = "o2oTransactionManager")
	public MainApply findMainApplyById(String id) {
		return this.proxyDao.getById(MainApply.class, id);
	}
	
	//查询主表 根据expandId
	@Transactional(transactionManager = "o2oTransactionManager")
	public MainApply findMainApplyByExpandId(String expandId) {
		String mainapplyNo = this.proxyDao.findString("select mainapply_no from main_apply where expand_id=?", expandId);
		if (mainapplyNo==null) {
			return null;
		}
		return this.proxyDao.getById(MainApply.class, mainapplyNo);
	}
	
	//修改主表
	@Transactional(transactionManager = "o2oTransactionManager")
	public int updateMainApplyByEntity(MainApply mainApply){
		return this.proxyDao.updateById(mainApply);
	}
	
	
	/**
	 * 修改工作流 状态
	 * 
	 * @param status
	 * @param loanapplyNo
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public void updateWorkFlowStatus(String status, String mainapplyNo) {
		if (status.equals("RUN")) {
			String sql = "update main_apply o set o.bill_status=?,o.submit_date=?,o.submit=?,submit_id=?,updator=?,updator_id=?,update_date=? where o.mainapply_no=?";
			this.proxyDao.execute(sql, status, userUtils.getCreateTime(), userUtils.getUserName(),userUtils.getCreateOr(),userUtils.getUserName(), userUtils.getUpdateOr(), userUtils.getUpdateTime(), mainapplyNo);
		} else {
			String sql = "update main_apply o set o.bill_status=?,updator=?,updator_id=?,update_date=?,end_date=? where o.mainapply_no=?";
			this.proxyDao.execute(sql, status,userUtils.getUserName(), userUtils.getUpdateOr(), userUtils.getUpdateTime(),userUtils.getUpdateTime(), mainapplyNo);
		}
	}
	
}
